//
//  MusicLyricStyleCell.swift
//  NDAi
//
//  Created by 丸子 on 2024/7/25.
//

import UIKit
import YYText
class MusicLyricStyleCell: UITableViewCell {
    var chooseDesc:((String) -> Void)?
    var chooseOther:((String) -> Void)?
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = Theme.color.main
        view.allRound(4)
        return view
    }()
    
    lazy var titleLb: UILabel = {
        let label = UILabel()
        label.font = UIFont.pingFangSCRegular(ofSize: 15)
        label.textColor = Theme.color.main
        let fullText = "歌词*"
        let attributedString = NSMutableAttributedString(string: fullText)
          // 查找 * 的范围并设置其颜色为红色
        if let range = fullText.range(of: "*") {
            let nsRange = NSRange(range, in: fullText)
            attributedString.addAttribute(.foregroundColor, value: UIColor.red, range: nsRange)
        }
        label.attributedText = attributedString
        return label
    }()
    
    lazy var descView: UIView = {
        let view = UIView()
        view.allRound(8)
        view.backgroundColor = .white
        return view
    }()
    
    lazy var descTextView: YYTextView = {
        let view = YYTextView()
        view.textContainerInset = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15);
        view.placeholderText = "输入歌词"
        view.placeholderFont = UIFont.pingFangSCRegular(ofSize: 14)
        view.placeholderTextColor = Theme.color.text_d3
        view.textColor = Theme.color.text_d1
        view.delegate = self
        view.font = UIFont.pingFangSCRegular(ofSize: 14)
        return view
    }()
    
    lazy var countLb: UILabel = {
        let label = UILabel()
        label.font = UIFont.pingFangSCSemibold(ofSize: 12)
        label.textColor = Theme.color.text_d2
        label.text = "0/500"
        return label
    }()
    
    lazy var buttonBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.imageForNormal = UIImage(named: "icon_music_auto")
        button.titleForNormal = "自动优化"
        button.titleFont = UIFont.pingFangSCRegular(ofSize: 14)
        button.titleColorForNormal = Theme.color.text_d1
        return button
    }()
    
    lazy var descTitleLb: UILabel = {
        let label = UILabel()
        label.font = UIFont.pingFangSCRegular(ofSize: 15)
        label.textColor = Theme.color.text_d1
        label.text = "纯音乐"
        return label
    }()

    lazy var switchBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.imageForNormal = UIImage(named: "icon_paint_nor")
        button.imageForSelected = UIImage(named: "icon_paint_sel")
        return button
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .clear
        selectionStyle = .none
            //, descTitleLb, twoDescLb, switchBtn, twoDescView
        contentView.addSubviews([lineView, titleLb, descView, descTitleLb, switchBtn])
        descView.addSubviews([descTextView, countLb, buttonBtn])
        
        lineView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.leading.equalTo(16)
            make.height.equalTo(16)
            make.width.equalTo(6)
        }
        
        titleLb.snp.makeConstraints { make in
            make.centerY.equalTo(lineView)
            make.leading.equalTo(lineView.snp.trailing).offset(8)
        }
        
        descView.snp.makeConstraints { make in
            make.top.equalTo(lineView.snp.bottom).offset(12)
            make.leading.trailing.equalToSuperview().inset(16)
            make.height.equalTo(130)
        }
        
        descTextView.snp.makeConstraints { make in
            make.leading.top.trailing.equalToSuperview()
            make.bottom.equalTo(buttonBtn.snp.top).offset(-6)
        }
        
        countLb.snp.makeConstraints { make in
            make.leading.bottom.equalToSuperview().inset(12)
        }
        
        buttonBtn.snp.makeConstraints { make in
            make.bottom.trailing.equalToSuperview().inset(12)
        }
        
        descTitleLb.snp.makeConstraints { make in
            make.centerY.equalTo(switchBtn)
            make.leading.equalTo(16)
        }
  
        switchBtn.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(0)
            make.trailing.equalToSuperview().offset(-15)
            make.width.equalTo(50)
            make.height.equalTo(24)
        }
        
        
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        kHideKeyboard()
    }
}

extension MusicLyricStyleCell: YYTextViewDelegate {
     
    func textViewDidChange(_ textView: YYTextView) {
        let currentLength = textView.text.count
        if textView == self.descTextView {
            if currentLength > 500 {
                textView.text = String(textView.text.prefix(500))
            }
            self.countLb.text = "\(textView.text.count)/500"
            self.chooseDesc?(textView.text)
        } else {
            if currentLength > 50 {
                textView.text = String(textView.text.prefix(50))
            }
            self.chooseOther?(textView.text)
        }
    }
    func textViewDidEndEditing(_ textView: YYTextView) {
         
    }
}
